package com.weixin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.weixin.config.WeiXinProperties;
import com.weixin.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zjd
 * @Title:
 * @Description:
 * @date 2021/4/1615:21
 */
@Api("企业微信-身份验证")
@RestController
@RequestMapping("/v1.0/wei-xin/cgi-bin")
public class LoginAuthController {
    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private ObjectMapper objectMapper;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("构造网页授权链接")
    @GetMapping("/query")
    public ResponseEntity get(@RequestParam("id") String id,
                              @RequestParam("code") String code,
                              @RequestParam("state") String state) throws IOException {

        System.out.println("跳转id="+id+"  code="+code+"  state="+state);
        return get(code);
    }

    @ApiOperation("登录")
    @GetMapping("/login")
    public ResponseEntity login(@RequestParam("code") String code,
                                @RequestParam("state") String state) throws IOException {
        System.out.println("跳转"+  "code="+code+"  state="+state);
        System.out.println("login");
        return   get(code);
    }



    public ResponseEntity get( String code) throws IOException {
        //https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/user/getuserinfo?");
        String access_token = (String) redisUtil.get("access_token_application");
        urlBuilder.append("access_token="+access_token);
        urlBuilder.append("&code="+code);
        String result = restTemplate.getForObject(urlBuilder.toString(), String.class);
        Map<String, Object> readValue = objectMapper.readValue(result, HashMap.class);
        return resultMap(readValue,1,null);
    }

    private ResponseEntity resultMap(Map<String, Object> resultMap, Integer type,String key){
        for (Map.Entry<String, Object> entry : resultMap.entrySet()) {
            if (entry.getKey().equals("errcode")){
                Integer integer = (Integer) resultMap.get("errcode");
                if (integer==0){
                    ResponseEntity responseEntity = type==0 ? new ResponseEntity(resultMap.get(key), HttpStatus.OK): new ResponseEntity(resultMap, HttpStatus.OK);
                    return responseEntity;
                }
            }
        }
        return new ResponseEntity(resultMap,HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
